package com.cxj.musicplayer.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cxj.musicplayer.MainActivity;
import com.cxj.musicplayer.R;
import com.cxj.musicplayer.dao.SongDBUtil;
import com.cxj.musicplayer.data.ActivityManager;
import com.cxj.musicplayer.data.DataManager;
import com.cxj.musicplayer.service.MediaService;

public class IndexActivity extends TitleActivity implements View.OnClickListener{
    LinearLayout love;
    LinearLayout web;
    TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_index);
        ActivityManager.activities.put(IndexActivity.class, this);

        love = findViewById(R.id.love);
        web = findViewById(R.id.web);

        love.setOnClickListener(this);
        web.setOnClickListener(this);

        textView = findViewById(R.id.love_count);

        SongDBUtil songDBUtil = new SongDBUtil(getBaseContext(), "music_db", null, 1);

        textView.setText(String.valueOf(songDBUtil.getLoveCount()));
    }

    @Override
    public void onClick(View view) {
        if(view.equals(love)){
            Intent intent = new Intent(this, LocalListActivity.class);
            ActivityManager.pre = getClass();
            startActivity(intent);
        }else{
            Intent intent = new Intent(this, MainActivity.class);
            ActivityManager.pre = getClass();
            startActivity(intent);
        }
    }

    @Override
    protected void onDestroy() {
        ActivityManager.activities.remove(getClass());
        MediaService.MusicController controller = (MediaService.MusicController) DataManager.concurrentHashMap_object_data.get("music_controller");
        controller.finish();
        controller = null;
        DataManager.concurrentHashMap_object_data.clear();
        DataManager.array_data.clear();
        DataManager.list_data.clear();
        DataManager.concurrentHashMap_data.clear();
        super.onDestroy();
    }

    @Override
    protected void onRestart() {
        super.onRestart();

        SongDBUtil songDBUtil = new SongDBUtil(getBaseContext(), "music_db", null, 1);

        textView.setText(String.valueOf(songDBUtil.getLoveCount()));
    }
}
